package com.lyk.coursearrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lyk.coursearrange.entity.Job;
import com.lyk.coursearrange.entity.Teacher;
import com.lyk.coursearrange.dao.TeacherDao;
import com.lyk.coursearrange.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lequal
 * @since 2020-03-13
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, Teacher> implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public Teacher teacherLogin(String username, String password) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper();
        wrapper.eq("teacher_no", username);
        wrapper.eq("password", password);
        return teacherDao.selectOne(wrapper);
    }

    @Override
    public List<Teacher> queryTeacher(Map<String, String> param) {
        Long page = null;
        Long limit = null;
        Long offset = null;

        if(param.get("page")!=null&&!param.get("page").equals("")){
            page = Long.valueOf(param.get("page"));
        }
        if(param.get("limit")!=null&&!param.get("limit").equals("")){
            limit = Long.valueOf(param.get("limit"));
        }
        if(page!=null&&limit!=null){
            offset =   (page - 1) * limit;
        }

        String realname = null;
        if(param.get("realname")!=null&&!param.get("realname").equals("")){
            realname = param.get("realname");
        }


        return  teacherDao.queryTeacher(offset,limit,realname);

    }

    @Override
    public Long teachercount(Map<String, String> param) {
        String realname = null;
        if(param.get("realname")!=null&&!param.get("realname").equals("")){
            realname = param.get("realname");
        }
        return teacherDao.teachercount(realname);
    }

    @Override
    public String getJobName(Long jobtitle) {
        return teacherDao.getJobName(jobtitle);
    }

    @Override
    public List<Job> queryTeacherJob() {
        return teacherDao.queryTeacherJob();
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        UpdateWrapper<Teacher> updateWrapper = new UpdateWrapper<>();
        System.out.println(teacher);
        System.out.println(teacher.getId());
        updateWrapper.eq("id",teacher.getId());
        return  update(teacher,updateWrapper);
    }

    @Override
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.addTeacher(teacher);
    }


}
